package com.qianfeng.zhangwei.sybl.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.qianfeng.zhangwei.sybl.R;
import com.qianfeng.zhangwei.sybl.adapter.GiftPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * post  数据
 */
public class GiftFragment extends BaseFragment implements ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener {
    private ViewPager viewPager;
    private List<Fragment> fragments = new ArrayList<>();

    private RadioGroup giftRadioGroup;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_gift, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        fragments.add(GameInfoFragment.newInstance("1"));
        fragments.add(GameInfoFragment.newInstance("2"));
        viewPager = (ViewPager) view.findViewById(R.id.fragment_gift_viewpager);
        viewPager.addOnPageChangeListener(this);
        GiftPagerAdapter adapter = new GiftPagerAdapter(getChildFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
        giftRadioGroup = (RadioGroup) view.findViewById(R.id.fragment_gift_rgs);
        giftRadioGroup.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.fragment_gift_moblie_rbtn) {
            viewPager.setCurrentItem(0);
        } else if (checkedId == R.id.fragment_gift_web_rbtn) {
            viewPager.setCurrentItem(1);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        RadioButton radioButton = (RadioButton) giftRadioGroup.getChildAt(position);
        radioButton.setChecked(true);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
